import requests
import json
import hashlib
import time


def get_channel_type(data):
    url = data['address'] + "/admin/merchantStatistics/supportPayinMethod"

    # 1. 准备请求数据
    payload = {"mid": data['mid']}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")


def get_payment_inquiry_pdf(data, order_id):
    # 代付的接口
    url = data['address'] + "/admin/payoutOrder/openInfo"
    print("url",url)
    print("order_id",order_id)

    # 1. 准备请求数据
    payload = {"mid": data['mid'], "downstream_sn": order_id}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")


def get_payment_inquiry_img(data, order_id):
    # 代付的接口
    url = data['address'] + "/admin/order/openInfo"
    print("url",url)
    print("order_id",order_id)

    # 1. 准备请求数据
    payload = {"mid": data['mid'], "downstream_sn": order_id}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")



def get_payment_inquiry_openmakeup(data, order_id,utr):
    # 查询代付订单补单接口
    url = data['address'] + "/admin/order/openMakeUp"
    print("url",url)
    print("order_id",order_id)

    # 1. 准备请求数据
    payload = {"mid": data['mid'], "downstream_sn": order_id,"utr":utr}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")

def get_order_submitted_backend(order, data):
    url = data['address'] + "/admin/order/openInfo"

    # 1. 准备请求数据
    payload = {"mid": data['mid'], "downstream_sn": order}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")



def get_reject(order, data):
    url = data['address'] + "/admin/payoutOrder/openReject"

    # 1. 准备请求数据
    payload = {"mid": data['mid'], "downstream_sn": order}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")


def get_exchange_rate_balance_distribution(data):
    url = data['address'] + "/admin/merchant/openInfo"
    print(url)

    # 1. 准备请求数据
    payload = {"mid": data['mid']}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")


def get_cash_success_rate(data,min):
    url = data['address'] + "/admin/merchantStatistics/openStat"
    # url = data['address'] + "/admin/merchantStatistics/openSucRate"
    # url = "https://admbr.deepay.ink/admin/merchantStatistics/openSucRate"


    # 1. 准备请求数据
    # minute = 1
    mid=data['mid']
    # payload = {"mid": mid, "minute": minute, "method": ""}  # 示例请求参数
    payload = {"mid": mid, "minute": min, "method": ""}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")



def get_large_success_rate(data,min):
    url = data['address'] + "/admin/merchantStatistics/openStat"
    # url = data['address'] + "/admin/merchantStatistics/openSucRate"
    # url = "https://admbr.deepay.ink/admin/merchantStatistics/openSucRate"


    # 1. 准备请求数据
    payload = {"minute": min, "method": ""}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")

def get_cash_in_interval_success_rate(data,min):
    url = data['address'] + "/admin/merchantStatistics/openStat"
    # url = "https://adm.deepay.ink/admin/merchantStatistics/openSucRate"


    # 1. 准备请求数据
    if min != '':
        minute = int(min)
    else:
        minute = 5

    mid=data['mid']
    # payload = f'''{{'mid':'{mid}','minute':'{minute}','method':''}}'''  # 示例请求参数
    payload = {"mid": mid, "minute": minute, "method": ""}
    print("payload",payload)
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")





def get_cash_in_day_success_rate(data):
    url = data['address'] + "/admin/merchantStatistics/openStat"
    # url = "https://adm.deepay.ink/admin/merchantStatistics/openSucRate"

    payload = {"mid": data['mid'], "minute": "1440", "method": ""}
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")



def get_cash_out_speed(data,min):
    url = data['address'] + "/admin/merchantStatistics/openStat"
    # url = "https://adm.deepay.ink/admin/merchantStatistics/openSucRate"


    # 1. 准备请求数据
    if min != '':
        minute = int(min)
    else:
        minute = 5

    mid=data['mid']
    # payload = f'''{{'mid':'{mid}','minute':'{minute}','method':''}}'''  # 示例请求参数
    payload = {"mid": mid, "minute": minute, "method": ""}
    print("payload",payload)
    payload_json = json.dumps(payload, separators=(',', ':'))  # 紧凑JSON格式

    # 2. 获取当前时间戳（毫秒级）
    timestamp = str(int(time.time() * 1000))  # 13位毫秒级时间戳
    # 或使用秒级：timestamp = str(int(time.time()))  # 10位秒级

    # 3. 设置AppID（需使用实际值，非任意值）
    appid = "123456"  # 替换为真实AppID
    ak = "09(o5PCgrswUU6BpJpfwukFZXiE@JKe2f"
    # 4. 生成签名（关键步骤）
    # 格式: md5(timestamp + payload + appid)
    sign_str = timestamp + payload_json + appid + ak
    x_sign = hashlib.md5(sign_str.encode('utf-8')).hexdigest()

    # 5. 构造请求头
    headers = {
        'x-ca-timestamp': timestamp,
        'x-ca-appid': appid,
        'x-sign': x_sign,
        'Content-Type': 'application/json'
    }

    # 6. 发送请求
    try:
        response = requests.post(
            url,
            headers=headers,
            data=payload_json  # 使用data而不是json
        )

        # 输出详细响应信息
        print(f"状态码: {response.status_code}")
        print("响应头:")
        for k, v in response.headers.items():
            print(f"  {k}: {v}")

        print("\n响应内容:")
        try:
            return json.dumps(response.json(), indent=2)
            print(json.dumps(response.json(), indent=2))
        except:
            print(response.text)

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")